#ifndef SOUNDMANAGER_CPP
#define SOUNDMANAGER_CPP

#include "soundmanager.h"

CSoundManager::CSoundManager()
{
#ifdef SOUND_ENABLE
    const char * melody[11] = {"do","do","do","re","mi2","re","do","mi2","re","re","do"};
    for (int i=0; i<11; i++)
    {
        m_auclairdelalune.push_back(melody[i]);
    }
    free(melody);
#endif
}

CSoundManager::~CSoundManager()
{
}

void CSoundManager::loadAllBuffer()
{
#ifdef SOUND_ENABLE
    std::cout << "Loading sounds..." << std::endl;
    // chargement des buffers

    sf::SoundBuffer buffer_tampon; // !!

    if (!buffer_tampon.LoadFromFile("snd/do.wav"))
        std::cerr << "N'a pas pu charger le son" << std::endl;
    m_buffer_map["do"] = buffer_tampon;

    if (!buffer_tampon.LoadFromFile("snd/re.wav"))
        std::cerr << "N'a pas pu charger le son" << std::endl;
    m_buffer_map["re"] = buffer_tampon;

    if (!buffer_tampon.LoadFromFile("snd/mi2.wav"))
        std::cerr << "N'a pas pu charger le son" << std::endl;
    m_buffer_map["mi2"] = buffer_tampon;

    if (!buffer_tampon.LoadFromFile("snd/fa.wav"))
        std::cerr << "N'a pas pu charger le son" << std::endl;
    m_buffer_map["fa"] = buffer_tampon;

    if (!buffer_tampon.LoadFromFile("snd/worms/laugh.wav"))
        std::cerr << "N'a pas pu charger le son" << std::endl;
    m_buffer_map["victory"] = buffer_tampon;

    if (!buffer_tampon.LoadFromFile("snd/worms/youllregretthat.wav"))
        std::cerr << "N'a pas pu charger le son" << std::endl;
    m_buffer_map["eaten"] = buffer_tampon;

    // chargement de la musique
    if (!m_music.OpenFromFile("snd/intro.ogg"))
        std::cerr << "N'a pas pu charger le son" << std::endl;

    std::cout << "Sounds loaded!" << std::endl;
#endif
}

void CSoundManager::pauseMusic()
{
#ifdef SOUND_ENABLE
    m_music.Pause();
#endif
}

void CSoundManager::play(const std::string& buffer_name)
{
#ifdef SOUND_ENABLE
    m_sound1.Stop();
    m_sound1.SetBuffer(m_buffer_map.find(buffer_name)->second);
    m_sound1.Play();
#endif
}

void CSoundManager::play(int n)
{
#ifdef SOUND_ENABLE
    n = n%11;
    play(m_auclairdelalune[n]);
#endif
}

void CSoundManager::playMusic()
{
#ifdef SOUND_ENABLE
    if ( m_music.GetStatus() != sf::Music::Playing ) m_music.Play();
#endif
}

void CSoundManager::stopMusic()
{
#ifdef SOUND_ENABLE
    m_music.Stop();
#endif
}

#endif //SOUNDMANAGER_CPP
